import axios from "axios";
const instance = axios.create({
  baseURL: "http://10.201.160.80:8001/data-cggc",
  timeout: 5000,
});
instance.interceptors.request.use(
  (config) => {
    config.headers["Content-Type"] = "application/json";
    return config;
  },
  (error) => {
    console.error("请求拦截器错误:", error);
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    // 对响应错误做点什么
    if (error.response) {
      console.log("服务器响应错误:", error.response.data);
      console.log("状态码:", error.response.status);
      console.log("响应头:", error.response.headers);
    } else if (error.request) {
      console.log("没有收到服务器响应:", error.request);
    } else {
      console.log("错误信息:", error.message);
    }
    console.log("配置信息:", error.config);
    return Promise.reject(error);
  }
);
function getPreviousDay(val) {
  const currentDate = new Date(val);
  const previousDay = new Date(currentDate);
  previousDay.setDate(previousDay.getDate() - 1);
  const year = previousDay.getFullYear();
  const month = String(previousDay.getMonth() + 1).padStart(2, "0");
  return `${year}-${month}`;
}
function getPreviouYae(val) {
  const currentDate = new Date(val);
  const previousDay = new Date(currentDate);
  previousDay.setDate(previousDay.getDate() - 1);
  const year = previousDay.getFullYear();
  const month = String(previousDay.getMonth() + 1).padStart(2, "0");
  const day = String(previousDay.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}
function timerOur(data) {
  const currentDate = new Date();
  const year = currentDate.getFullYear();
  const month =
    currentDate.getMonth() + 1 > 9
      ? `${currentDate.getMonth() + 1}`
      : `0${currentDate.getMonth() + 1}`;
  const day =
    currentDate.getDate() > 9
      ? `${currentDate.getDate()}`
      : `0${currentDate.getDate()}`;
  switch (true) {
    case data == "日":
      return { startTime: getPreviouYae(`${year}-${month}-${day}`) };
    case data == "月":
      return { startTime: getPreviousDay(`${year}-${month}`) };
    case data == "季":
      return { startTime: year + "Q1" };
    case data == "年":
      return { startTime: year };
    default:
      return data;
  }
}

const postRequest = async (url, data) => {
  try {
    const response = await instance.post(url, timerOur(data));
    return response;
  } catch (error) {
    throw error;
  }
};

function getPreviousSixMonths(val) {
  const currentDate = new Date(val);
  const months = [];
  for (let i = 1; i <= 6; i++) {
    const newDate = new Date(currentDate);
    newDate.setMonth(newDate.getMonth() - i);
    const year = newDate.getFullYear();
    const month = String(newDate.getMonth() + 1).padStart(2, "0");
    months.push(`${year}-${month}`);
  }
  return months;
}

const InitReceivables = async (url, data) => {
  try {
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month =
      currentDate.getMonth() + 1 > 9
        ? `${currentDate.getMonth() + 1}`
        : `0${currentDate.getMonth() + 1}`;
    let startTimeList = data
      ? [year]
      : getPreviousSixMonths(`${year}-${month}`);
    const response = await instance.post(url, { startTimeList });
    return response;
  } catch (error) {
    throw error;
  }
};
export { postRequest, InitReceivables };
